package com.more.offer.algorithm.leetcode.lcode283;

/**
 * @author pinoc
 * @date 2021/8/8
 * @description leetcode 283题
 * 给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序
 * 输入: [0,1,0,3,12]
 * 输出: [1,3,12,0,0]
 */
public class Test {

	public static void main(String[] args) {
		int num[] = {1,2,0,4,5,0,0,0,};
		sortByZero(num);

		for (int i = 0; i < num.length; i++) {
			System.out.print(num[i]+" ");
		}
	}

	public static void sortByZero(int num[]) {
		int j = 0;
		for (int i = 0; i < num.length; i++) {
			if(num[i] != 0) {
				num[j] = num[i];
				if(j != i) {
					num[i] = 0;
				}
				j++;
			}
		}
	}
}
